package com.hyt.it.ogt.ykcj.service.analysis.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyt.it.ogt.ykcj.common.enums.CommonEnums;
import com.hyt.it.ogt.ykcj.common.exception.CustomException;
import com.hyt.it.ogt.ykcj.common.utils.DateUtils;
import com.hyt.it.ogt.ykcj.common.utils.IdUtils;
import com.hyt.it.ogt.ykcj.domain.analysis.ExamScoreAnalysisBlockLine;
import com.hyt.it.ogt.ykcj.domain.analysis.ExamScoreAnalysisLine;
import com.hyt.it.ogt.ykcj.domain.vo.analysis.ScoreLinePointVo;
import com.hyt.it.ogt.ykcj.mapper.analysis.ExamScoreAnalysisBlockLineLMapper;
import com.hyt.it.ogt.ykcj.mapper.analysis.ExamScoreAnalysisLineLMapper;
import com.hyt.it.ogt.ykcj.service.analysis.IExamScoreAnalysisBlockLineService;
import com.hyt.it.ogt.ykcj.service.analysis.IExamScoreAnalysisLineService;
import com.hyt.loginfo.service.IBaseLoginService;

import lombok.extern.slf4j.Slf4j;

/**
 * description
 * 考试分数区间范围类
 *
 * @author yaojian
 * @createTime 2021/12/31
 */
@Slf4j
@Service
public class ExamScoreAnalysisLineServiceImpl implements IExamScoreAnalysisLineService {

    @Autowired
    private IExamScoreAnalysisBlockLineService examScoreAnalysisBlockLineService;
    @Autowired
    private ExamScoreAnalysisLineLMapper examScoreAnalysisLineLMapper;
    @Autowired
    private ExamScoreAnalysisBlockLineLMapper examScoreAnalysisBlockLineLMapper;
    @Autowired
    private IBaseLoginService iBaseLoginService;

    @Override
    public ScoreLinePointVo selectScoreLineById(String examId) {

        //INFO 先获取分数段，然后获取分数节点
        QueryWrapper<ExamScoreAnalysisLine> wrapper = new QueryWrapper();
        wrapper.lambda().eq(ExamScoreAnalysisLine::getExamId, examId);
        //获取分数段
        ExamScoreAnalysisLine examScoreAnalysisLine = examScoreAnalysisLineLMapper.selectOne(wrapper);
        ScoreLinePointVo scoreLinePointVo = new ScoreLinePointVo();

        if (examScoreAnalysisLine != null && StringUtils.isNotEmpty(examScoreAnalysisLine.getId())) {
            scoreLinePointVo.setMaxScore(examScoreAnalysisLine.getMaxScore());
            scoreLinePointVo.setMinScore(examScoreAnalysisLine.getMinScore());
            scoreLinePointVo.setExamId(examScoreAnalysisLine.getExamId());

            QueryWrapper<ExamScoreAnalysisBlockLine> queryWrapper = new QueryWrapper();
            queryWrapper.lambda().eq(ExamScoreAnalysisBlockLine::getLineId, examScoreAnalysisLine.getId());
            //获取分数节点
            List<ExamScoreAnalysisBlockLine> examScoreAnalysisBlockLines = examScoreAnalysisBlockLineLMapper.selectList(queryWrapper);
            List<Integer> pointScore = new ArrayList<>();
            examScoreAnalysisBlockLines.stream().forEach(examScoreAnalysisBlockLine -> {
                if (examScoreAnalysisBlockLine.getLast().equals(CommonEnums.LAST_0.getCode())) {
                    pointScore.add(new Integer(examScoreAnalysisBlockLine.getPoint()));
                }
            });
            scoreLinePointVo.setPointScoreList(pointScore);
        }
        return scoreLinePointVo;
    }

    @Transactional
    @Override
    public boolean saveOrUpdate(ScoreLinePointVo scoreLinePointVo) {
        validData(scoreLinePointVo);
        //看是否存要修改的数据
        QueryWrapper<ExamScoreAnalysisLine> LineWrapper = new QueryWrapper();
        LineWrapper.lambda().eq(ExamScoreAnalysisLine::getExamId, scoreLinePointVo.getExamId());
        ExamScoreAnalysisLine selectExamScoreAnalysisLine = examScoreAnalysisLineLMapper.selectOne(LineWrapper);
        if (selectExamScoreAnalysisLine == null || StringUtils.isEmpty(selectExamScoreAnalysisLine.getId())) {
            //如果不存在，则进行添加
            selectExamScoreAnalysisLine = new ExamScoreAnalysisLine();
            String ScoreLineid = IdUtils.generateId();
            selectExamScoreAnalysisLine.setId(ScoreLineid);
            selectExamScoreAnalysisLine.setId(ScoreLineid);
            selectExamScoreAnalysisLine.setMaxScore(scoreLinePointVo.getMaxScore());
            selectExamScoreAnalysisLine.setMinScore(scoreLinePointVo.getMinScore());
            selectExamScoreAnalysisLine.setExamId(scoreLinePointVo.getExamId());
            selectExamScoreAnalysisLine.setCreateBy(iBaseLoginService.getSysUser().getLoginname());
            selectExamScoreAnalysisLine.setCreateTime(DateUtils.getNowDate());
            selectExamScoreAnalysisLine.setUpdateBy(iBaseLoginService.getSysUser().getLoginname());
            selectExamScoreAnalysisLine.setUpdateTime(DateUtils.getNowDate());
            selectExamScoreAnalysisLine.setOfficeId(iBaseLoginService.getOfficeId());
            examScoreAnalysisLineLMapper.insert(selectExamScoreAnalysisLine);
        } else {
            //存在进行修改
            selectExamScoreAnalysisLine.setId(selectExamScoreAnalysisLine.getId());
            selectExamScoreAnalysisLine.setMaxScore(scoreLinePointVo.getMaxScore());
            selectExamScoreAnalysisLine.setMinScore(scoreLinePointVo.getMinScore());
            selectExamScoreAnalysisLine.setUpdateBy(iBaseLoginService.getSysUser().getLoginname());
            selectExamScoreAnalysisLine.setUpdateTime(DateUtils.getNowDate());
            selectExamScoreAnalysisLine.setOfficeId(iBaseLoginService.getOfficeId());
            examScoreAnalysisLineLMapper.updateById(selectExamScoreAnalysisLine);
        }
        //2.保存分数段节点
        //2.1.删除全部的分数段
        QueryWrapper<ExamScoreAnalysisBlockLine> BlockLineWrapper = new QueryWrapper();
        BlockLineWrapper.lambda().eq(ExamScoreAnalysisBlockLine::getLineId, selectExamScoreAnalysisLine.getId());
        examScoreAnalysisBlockLineService.remove(BlockLineWrapper);
        int currLessScore = 0;
        List<ExamScoreAnalysisBlockLine> examScoreAnalysisBlockLines = new ArrayList<>();
        for (int i = 0; i < scoreLinePointVo.getPointScoreList().size(); i++) {
            ExamScoreAnalysisBlockLine examScoreAnalysisBlockLine = new ExamScoreAnalysisBlockLine();
            examScoreAnalysisBlockLine.setLineId(selectExamScoreAnalysisLine.getId());
            examScoreAnalysisBlockLine.setId(IdUtils.generateId());
            examScoreAnalysisBlockLine.setPoint(scoreLinePointVo.getPointScoreList().get(i));
            examScoreAnalysisBlockLine.setLast(CommonEnums.LAST_0.getCode());
            examScoreAnalysisBlockLine.setCreateBy(iBaseLoginService.getSysUser().getLoginname());
            examScoreAnalysisBlockLine.setCreateTime(DateUtils.getNowDate());
            examScoreAnalysisBlockLine.setUpdateBy(iBaseLoginService.getSysUser().getLoginname());
            examScoreAnalysisBlockLine.setUpdateTime(DateUtils.getNowDate());
            examScoreAnalysisBlockLine.setOfficeId(iBaseLoginService.getOfficeId());
            if (i == 0) {
                currLessScore = scoreLinePointVo.getMinScore();
            } else {
                currLessScore = scoreLinePointVo.getPointScoreList().get(i - 1);
            }
            examScoreAnalysisBlockLine.setGreaterPoint(currLessScore);
            examScoreAnalysisBlockLines.add(examScoreAnalysisBlockLine);
        }
        //2.2保存最后一个点
        ExamScoreAnalysisBlockLine examScoreAnalysisBlockLine = new ExamScoreAnalysisBlockLine();
        examScoreAnalysisBlockLine.setLineId(selectExamScoreAnalysisLine.getId());
        examScoreAnalysisBlockLine.setId(IdUtils.generateId());
        examScoreAnalysisBlockLine.setPoint(scoreLinePointVo.getMaxScore());
        examScoreAnalysisBlockLine.setLast(CommonEnums.LAST_1.getCode());
        examScoreAnalysisBlockLine.setCreateBy(iBaseLoginService.getSysUser().getLoginname());
        examScoreAnalysisBlockLine.setCreateTime(DateUtils.getNowDate());
        examScoreAnalysisBlockLine.setUpdateBy(iBaseLoginService.getSysUser().getLoginname());
        examScoreAnalysisBlockLine.setUpdateTime(DateUtils.getNowDate());
        examScoreAnalysisBlockLine.setOfficeId(iBaseLoginService.getOfficeId());
        examScoreAnalysisBlockLine.setGreaterPoint(scoreLinePointVo.getPointScoreList().get(scoreLinePointVo.getPointScoreList().size() - 1));
        examScoreAnalysisBlockLines.add(examScoreAnalysisBlockLine);
        examScoreAnalysisBlockLineService.saveBatch(examScoreAnalysisBlockLines);

        return false;
    }

    /**
     * 校验参数
     *
     * @param scoreLinePointVo
     */
    private void validData(ScoreLinePointVo scoreLinePointVo) {
        //最大和最小值不能相同
        if (scoreLinePointVo.getMaxScore() == scoreLinePointVo.getMinScore()) {
            throw new CustomException("最大极限值和最小极限值不能相同！");
        }
        //最大的要大于最小的
        if (scoreLinePointVo.getMaxScore() < scoreLinePointVo.getMinScore()) {
            throw new CustomException("最大极限值应大于最小极限值！");
        }
        //需要设置分数段
        if (scoreLinePointVo.getPointScoreList() == null || scoreLinePointVo.getPointScoreList().size() <= 0) {
            throw new CustomException("请设置分数段节点！");
        }
        //不能操过10个
        if (scoreLinePointVo.getPointScoreList() != null && scoreLinePointVo.getPointScoreList().size() > 10) {
            throw new CustomException("不能超过10个分数段节点！");
        }
        //不能重复
        HashSet<Integer> hashSet = new HashSet<>(scoreLinePointVo.getPointScoreList());
        if (scoreLinePointVo.getPointScoreList().size() != hashSet.size()) {
            throw new CustomException("分数段存节点存在重复数据！");
        }
        //所有值都应该大于最小极限值和小于最大极限值
        scoreLinePointVo.getPointScoreList().forEach(num -> {
            if (scoreLinePointVo.getMinScore() >= num || scoreLinePointVo.getMaxScore() <= num) {
                throw new CustomException("分数段存节点需要设置到分数段范围内！");
            }
        });
        //排序
        Collections.sort(scoreLinePointVo.getPointScoreList());
    }
}
